$(function(){
    
    
    /**
     * if we get data - we get (ajax) data with database
     */   
    $.ajax
    ({
       type: 'GET',
       url: 'dashboard/xhrGetListings',
       data: null,
       datatype: 'json',
       success: function(obj)
       {
            var json=JSON.parse(obj);
            for(var i=0; i < json.length; i++)
            {
                $('#listInserts').append('<div><a class="del" rel="' + json[i].id + '" href="#">X</a>' + json[i].text + '</div>'); 
            }
            
            /**
            *  del action
            */
           $('.del').live('click', function()
           {
                delItem = $(this);
               var id = $(this).attr('rel');

               // alert(id);

                //delete action
               $.post('dashboard/xhrDelListing', {'id' : id}, function(obj)
               {
                      delItem.parent().remove();
                });
           });

            
       }
    });
    
    
    
    
    
    /**
     *  if we insert data - we will be dislpay this data of page
     */
    $('#randomInsert').submit(function()
    {
       var url = $(this).attr('action'); 
       var data = $(this).serialize();
       
       $.post(url, data, function(obj){
          
          var json = JSON.parse(obj);
          
          // alert(json.id);
           $('#listInserts').append('<div><a class="del" rel="' + json.id + '" href="#">X</a>' + json.text + '</div>');
       });
       return false;
    });
    
});